package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends BaseServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Override
    public ResponseResult list(SensitiveDto dto) {
        if (dto == null) return errorParam;
        LambdaQueryWrapper<AdSensitive> qw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            qw.like(AdSensitive::getSensitives, dto.getName());
        }
        return getPage(dto, qw);
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        if (adSensitive == null) return errorParam;
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return success;
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        if (adSensitive == null || adSensitive.getId() == null) return errorParam;
        updateById(adSensitive);
        return success;
    }

    @Override
    public ResponseResult deleteByid(Integer id) {
        if (id == null) return errorParam;
        AdSensitive byId = getById(id);
        if (byId == null) return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        deleteByid(id);
        return success;
    }
}
